package com.ybfenpan.controller.member.ti;

import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Member;
import com.ybfenpan.model.ParamWage;
import com.ybfenpan.model.log.LogMoneyTi;
import com.ybfenpan.service.DailiService;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.service.ParamWageService;
import com.ybfenpan.service.log.LogMoneyTiService;
import com.ybfenpan.taglib.annotation.Token;
import com.ybfenpan.utils.ContextUtil;
import com.ybfenpan.utils.CryptUtil;
import com.ybfenpan.utils.MapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;


@Controller

public class TiController extends BaseController {

    @Resource
    private MemberService memberService;
    @Resource
    private LogMoneyTiService logMoneyTiService;
    @Resource
    private DailiService dailiService;

    @Resource
    private ParamWageService paramWageService;

    @Token(save = true)
    @RequestMapping("member/ti/tiShow.shtml")
    public String show(HttpServletRequest request) throws Exception {

        //代理商信息
        Member member = ContextUtil.getMember();
        member = memberService.getByKey(member.getId());
        request.setAttribute("member", member);

        ParamWage paramWage1011 = paramWageService.getByKey(1011);
        request.setAttribute("paramWage1011", paramWage1011.getValue());

        ParamWage paramWage1012 = paramWageService.getByKey(1012);
        request.setAttribute("paramWage1012", paramWage1012.getValue());

        ParamWage paramWage1017 = paramWageService.getByKey(1017);
        request.setAttribute("paramWage1017", paramWage1017.getValue());

        request.setAttribute("bankName", member.getBankName());
        request.setAttribute("bankPersonName", member.getBankPersonName());
        request.setAttribute("bankCardNo", member.getBankCardNo());
        request.setAttribute("bankSheng", member.getBankSheng());
        request.setAttribute("bankShi", member.getBankShi());
        request.setAttribute("bankAddress", member.getBankAddress());

        return "member/ti/ti";
    }

    @PostMapping("member/ti/ti.shtml")
    @ResponseBody
    @Token(remove = true)
    public void ti(HttpServletRequest request, ModelMap model, HttpServletResponse response) throws Exception {

        Boolean result = true;
        String msg = "";
        //代理商信息
        Member member = ContextUtil.getMember();
        member = memberService.getByKey(member.getId());

        Map<String, Object> mapParamWage = new HashMap<String, Object>();
        mapParamWage.put("where", "1=1 and id in (1003,1011,1012,1017) ");
        List<ParamWage> paramWageList = this.paramWageService.queryForList("select", mapParamWage);

        Map<Integer, ParamWage> paramWageMap = new HashMap<Integer, ParamWage>();
        for (ParamWage p : paramWageList) {
            paramWageMap.put(p.getId(), p);
        }

        Map formMap = getParameterMap(request);
        String myTwoPassword = MapUtil.getString(formMap, "myTwoPassword");
        String pwd = CryptUtil.getInstance().encrypt2String(myTwoPassword, CryptUtil.keyStr);

        if (!member.getTwoPassword().equals(pwd)) {
            msg = "二级密码不正确。";
            result = false;
        } else {
            Double num = MapUtil.getDoubleValue(formMap, "num");
            int type = MapUtil.getIntValue(formMap, "type");
            if (num <= 0) {
                msg = "提现金额填写不正确";
                result = false;
            } else {

                if (num < paramWageMap.get(1011).getValue()) {
                    msg = "提现金额必须大于：" + paramWageMap.get(1011).getValue();
                    result = false;
                }


                if ((num % paramWageMap.get(1012).getValue()) != 0) {

                    msg = "提现金额必须是" + paramWageMap.get(1012).getValue() + "的整数！";
                    result = false;
                }


                if (member.getMoney() < num) {
                    msg = "现金积分不足，提现金额：" + num;
                    result = false;
                }

                if (result) {

                    Double fei = num * paramWageMap.get(1017).getValue() * 0.01;

                    LogMoneyTi logMoneyTi = new LogMoneyTi();
                    //订单编号
                    Random random = new Random();
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
                    String tiCodeNo = "T" + formatter.format(new Date()) + random.nextInt(1) + random.nextInt(1);

                    logMoneyTi.setTiCodeNo(tiCodeNo);

                    logMoneyTi.setMemberId(member.getId());
                    logMoneyTi.setNum(num);
                    logMoneyTi.setFei(fei);
                    logMoneyTi.setShi(num - fei);
                    logMoneyTi.setType(type);
                    logMoneyTi.setBankPersonName(MapUtil.getString(formMap, "bankPersonName"));
                    logMoneyTi.setBankName(MapUtil.getString(formMap, "bankName"));
                    logMoneyTi.setBankCardNo(MapUtil.getString(formMap, "bankCardNo"));
                    logMoneyTi.setBankAddress(MapUtil.getString(formMap, "bankAddress"));
                    logMoneyTi.setBankSheng(MapUtil.getString(formMap, "bankSheng"));
                    logMoneyTi.setBankShi(MapUtil.getString(formMap, "bankShi"));
                    logMoneyTi.setBankPersonName(MapUtil.getString(formMap, "bankPersonName"));
                    logMoneyTi.setZhifuNo(MapUtil.getString(formMap, "zhifuCardNo"));
                    logMoneyTi.setZhifuPersonName(MapUtil.getString(formMap, "zhifuName"));
                    logMoneyTiService.saveTi(logMoneyTi);

                }else {
                    request.setAttribute("member", formMap);
                }

            }

        }

        HashMap resultformMap = new HashMap();
        resultformMap.put("msg", msg);
        renderSuccess(response, result, resultformMap);

    }


}
